# format文件系统大小
format = (size) ->
    if size >= 1024*1024*1024
        return (size/(1024*1024*1024)).toFixed(2) + ' GB'
    else if 1024*1024*1024 > size and size >= 1024*1024
        return (size/(1024*1024)).toFixed(2) + ' MB'
    else
        return (size/1024).toFixed(2) + ' KB'

length = 0
unit = ''

format_num = (size) ->
    if size >= 1024*1024*1024
        length = 1024*1024*1024
        unit = 'GB'
        return (size/(1024*1024*1024)).toFixed(2)
    else if 1024*1024*1024 > size and size >= 1024*1024
        length = 1024*1024
        unit = 'MB'
        return (size/(1024*1024)).toFixed(2)
    else
        length = 1024
        unit = 'KB'
        return (size/1024).toFixed(2)

$ ->
    # 引入并解析window对象
    network_stats = JSON.parse(window.network_stats)

    # 基于准备好的dom，初始化echarts实例
    network_stats_chart = echarts.init(document.getElementById('network_stats'))

    # 总接收和总发送
    all_rx = 0
    for item in network_stats
        all_rx += item.rx
    format_rx = format all_rx

    all_tx = 0
    for item in network_stats
        all_tx += item.tx
    format_tx = format all_tx

    # 指定图表的配置项和数据
    option = {
        title: {
            text: '网络使用情况',
            subtext: '已接收：' + format_rx + '，已发送：' + format_tx,
            subtextStyle: {
                color: 'blue'
            }
            x: 'center',
            padding: [0,0]
        },
        tooltip : {
            trigger: 'axis',
            axisPointer : {            # 坐标轴指示器，坐标轴触发有效
                type : 'shadow'        # 默认为直线，可选为：'line' | 'shadow'
            }
            formatter: (params) ->
                # 判断接口状态
                if not network_stats[params[0].dataIndex]
                    return '未知'
                else
                    if network_stats[params[0].dataIndex].operstate == 'up'
                        rx = format params[0].value
                        tx = format params[1].value
                        return '接口名称：' + params[0].name + '<br/>' +
                                params[0].seriesName + '：' + rx  + '<br/>' +
                                params[1].seriesName + '：' + tx
                    else if network_stats[params[0].dataIndex].operstate == 'down'
                        return '接口已禁用'
        },
        legend: {
            orient: 'vertical',
            x: 'left',
            data:['已接收', '已发送']
        },
        grid: {
            left: '3%',
            right: '20%',
            bottom: '3%',
            top: '25%',
            containLabel: true
        },
        xAxis: [ {
            type: 'value',
            # nameLocation: 'start',
            # nameRotate: 15,
            name: '(B)',
            axisLabel: {
                # rotate: -15,
                formatter: (value,index) ->
                    sub_label = (value/length).toFixed(2)
                    return sub_label
            }
        } ],
        yAxis: [ {
            type: 'category',
            axisTick: {show: false},
            data: [],
            name: '网络接口'
        } ],
        series: [ {
            name: '已接收',
            type: 'bar',
            barCategoryGap: '30%',
            barWidth: '20px',
            label: {
                normal: {
                    show: true,
                    position: 'right',
                    formatter: (params) ->
                        value = format params.value
                        return value
                }
            },
            itemStyle: {
                normal: {
                    color: '#00A600'
                }
            },
            data: []
        }, {
            name: '已发送',
            type: 'bar',
            barCategoryGap: '30%',
            barWidth: '20px',
            label: {
                normal: {
                    show: true,
                    position: 'right',
                    formatter: (params) ->
                        value = format params.value
                        return value
                }
            },
            itemStyle: {
                normal: {
                    color: '#AE0000'
                }
            },
            data: []
        } ]
    }

    # 遍历network_stats，获取网络统计数据
    all_value = []
    for item,index in network_stats
        option.yAxis[0].data[index]  = item.iface
        option.series[0].data[index] = item.rx
        option.series[1].data[index] = item.tx
        all_value.push(item.rx)
        all_value.push(item.tx)
    max = Math.max.apply Math, all_value
    format_num max
    option.xAxis[0].name = unit

    # 改变X轴的最大坐标值
    # option.xAxis[0].max = max

    # 使用刚指定的配置项和数据显示图表
    network_stats_chart.setOption(option)
